package com.smart.translator.ui;

import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.smart.translator.R;
import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
    private static final String TAG = "MessageAdapter";
    
    public static class Message {
        public boolean isMe; // true: 我方，false: 对方
        public String sourceText;
        public String translatedText;
        public boolean isSpeaking; // 新增：对方正在讲话
        public Message(boolean isMe, String sourceText, String translatedText) {
            this(isMe, sourceText, translatedText, false);
        }
        public Message(boolean isMe, String sourceText, String translatedText, boolean isSpeaking) {
            this.isMe = isMe;
            this.sourceText = sourceText;
            this.translatedText = translatedText;
            this.isSpeaking = isSpeaking;
        }
    }

    private final List<Message> messageList;

    public MessageAdapter(List<Message> messageList) {
        this.messageList = messageList;
    }

    @NonNull
    @Override
    public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
        Message msg = messageList.get(position);
        
        if (msg.isMe) {
            holder.layoutLeft.setVisibility(View.GONE);
            holder.layoutRight.setVisibility(View.VISIBLE);
            holder.tvRightSource.setText(msg.sourceText);
            holder.tvRightSource.setPaintFlags(holder.tvRightSource.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
            holder.tvRightTrans.setText(msg.translatedText);
        } else {
            holder.layoutLeft.setVisibility(View.VISIBLE);
            holder.layoutRight.setVisibility(View.GONE);
            holder.tvLeftSource.setText(msg.sourceText);
            holder.tvLeftSource.setPaintFlags(holder.tvLeftSource.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
            holder.tvLeftTrans.setText(msg.translatedText);
            
            // 新增：对方正在讲话提示
            View speakingTip = holder.layoutLeft.findViewById(R.id.layout_left_speaking);
            if (msg.isSpeaking) {
                speakingTip.setVisibility(View.VISIBLE);
            } else {
                speakingTip.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }

    static class MessageViewHolder extends RecyclerView.ViewHolder {
        LinearLayout layoutLeft, layoutRight;
        TextView tvLeftSource, tvLeftTrans, tvRightSource, tvRightTrans;
        MessageViewHolder(@NonNull View itemView) {
            super(itemView);
            layoutLeft = itemView.findViewById(R.id.layout_left);
            layoutRight = itemView.findViewById(R.id.layout_right);
            tvLeftSource = itemView.findViewById(R.id.tv_left_source);
            tvLeftTrans = itemView.findViewById(R.id.tv_left_trans);
            tvRightSource = itemView.findViewById(R.id.tv_right_source);
            tvRightTrans = itemView.findViewById(R.id.tv_right_trans);
        }
    }
} 